/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.AttributeTypeHome;
import com.iti.jets.gpts.home.TranServiceHome;
import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.pojo.TranService;
import com.iti.jets.gpts.pojo.TranServiceAttribute;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Hibernate;

/**
 *
 * @author U
 */
@Path("/attribute")
public class AtrributeClass {
    TranServiceHome tsh=new TranServiceHome(util.HibernateUtil.getSessionFactory());
    AttributeTypeHome dao = new AttributeTypeHome(util.HibernateUtil.getSessionFactory());

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/add/{id}/{name}/{isnumber}")
    public String insertNewAttribute(@PathParam(value = "id") int id, @PathParam(value = "name") String name, @PathParam(value = "isnumber") boolean isnumber) {

        boolean response = dao.isAttributeExist(name);
        if (response == true) {

            return "invalid";
        } else {
            AttributeType attributeType1 = new AttributeType();
            attributeType1.setId(id);
            attributeType1.setIsNumber(isnumber);
            attributeType1.setName(name);
            dao.create(attributeType1);
            return "valid";
        }
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/update/{id}/{name}/{isbumber}")
    public String UpdateAttribute(@PathParam(value = "id") int id, @PathParam(value = "name") String name, @PathParam(value = "isbumber") boolean isnumber) {

        AttributeType attributeType = dao.getObject(id);
        attributeType.setIsNumber(isnumber);
        attributeType.setName(name);
        dao.update(attributeType);
        return "Done";
    }

    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/remove/{id}")
    public String RemoveAttribute(@PathParam(value = "id") int id) {
        System.out.println("da5al remove shimaaaaaaaaaaaaaaaaaaaa");
        AttributeType attributeType = new AttributeType();
        attributeType = dao.getObject(id);
        System.out.println(attributeType.getName());
        dao.delete(attributeType);
        return "done!";
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/all")
    public JSONArray getAllAttribute() throws JSONException {

        List<AttributeType> result = dao.loadAll();
        JSONArray jsonAttributeArray = new JSONArray();

        for (AttributeType attributeType : result) {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("id", attributeType.getId());
            jSONObject.put("name", attributeType.getName());
            jSONObject.put("isnumber", attributeType.getIsNumber());
            jsonAttributeArray.put(jSONObject);
        }
        return jsonAttributeArray;
    }

    @Path("/gettsa/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public JSONArray gettsa(@PathParam(value = "id") int id) throws JSONException {
        TranService read = tsh.read(id);
        Hibernate.initialize(read);
        JSONArray jSONArray = new JSONArray();
        Iterator iterator = read.getTranServiceAttributes().iterator();
        while (iterator.hasNext()) {
            TranServiceAttribute tsa = (TranServiceAttribute) iterator.next();
            JSONObject jSONtsa = new JSONObject();
            //id of Transervice
            jSONtsa.put("id", tsa.getId());
            jSONtsa.put("value", tsa.getValue());
            JSONObject jSONtstat = new JSONObject();
            //id of tran_service_type_attribute
            jSONtstat.put("id", tsa.getTranServiceTypeAttributeType().getId());
            JSONObject jSONat = new JSONObject();
            jSONat.put("name", tsa.getTranServiceTypeAttributeType().getAttributeType().getName());
            jSONat.put("id", tsa.getTranServiceTypeAttributeType().getAttributeType().getId());
            jSONat.put("is_number", tsa.getTranServiceTypeAttributeType().getAttributeType().getIsNumber());
            jSONtstat.put("at", jSONat);
            jSONtsa.put("tstat", jSONtstat);
            jSONArray.put(jSONtsa);
        }
        return jSONArray;
    }
}
